import { reactive, inject } from "vue";
import { useApproveStore } from '../../store/useApproveStore'

export const useApproveTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;
    const approveTable = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                approveName: '',
                flowTypeId: '',
                submitUserName: ''
            },
            approveRecord: null,
            checkedRecords: [],
            nextNode:null,
        }
    );

    const approveStore = useApproveStore();

    //查询
    const getAllApproveMain = async (currentUserId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Approve.GetAllApproveMain,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: {
                        submitUserId: currentUserId,
                        approveName: approveTable.queryCondition.approveName,
                        flowTypeId: approveTable.queryCondition.flowTypeId
                    },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                approveTable.tableData = response.data.data;
                approveTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //获取待办流程
    const getWaitToBeDoneApprove = async (currentUserId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Approve.GetWaitToBeDoneApproveMain,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: {
                        approveUserId: currentUserId,
                        isCheck: 0
                    },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                approveTable.tableData = response.data.data;
                approveTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    }

    //新增
    const createData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Approve.Create,
                approveStore.approveMainModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Approve.Update,
                approveStore.approveMainModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新项目指定信息
    const updateApproveSpecificContent = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Approve.UpdateApproveMainSpecificContent,
                approveStore.approveMainModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //根据外部流程ID删除审批记录
    const deleteDataByFid = async (fid) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Approve.DeleteApproveMainByFId,
                { 'fid': fid }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //根据节点ID删除审批记录
    const deleteApproveRecordByNodeId = async (nodeId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Approve.DeleteApproveRecordByNodeId,
                { 'nodeId': nodeId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //审批流程
    const doApprove = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Approve.DoApprove,
                approveStore.approveRecordModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
        return 0;
    }

    //获取当前用户，当前流程的待审批信息
    const getApproveInfo = async (approveUserId, pId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Approve.GetWaitToBeDownRecord,
                {
                    approveUserId: approveUserId, pId: pId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                approveTable.approveRecord = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }
    // 当前流程所有已审核的审批记录
    const getAllCheckedRecordsByPId = async (pId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Approve.GetAllCheckedRecordsByPId,
                {
                    pId: pId
                }
            );
            
            if (response.status == "200" && response.data.code == "200") {
                approveTable.checkedRecords = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    // 当前流程所有已审核的审批记录
    const getNextFlowNode = async (pId, flowId, nodeId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Approve.GetNextFlowNode,
                {
                    approveId: pId,
                    flowId: flowId,
                    nodeId: nodeId
                }
            );
            
            if (response.status == "200" && response.data.code == "200") {
                approveTable.nextNode = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    return {
        approveTable,
        approveStore,
        getAllApproveMain,
        getWaitToBeDoneApprove,
        createData,
        updateData,
        deleteDataByFid,
        deleteApproveRecordByNodeId,
        doApprove,
        getApproveInfo,
        getAllCheckedRecordsByPId,
        updateApproveSpecificContent,
        getNextFlowNode
    };
};